<template>
  <div id="app">
    <img src="./assets/logo.png" alt="" />
    <h1>{{ count }}</h1>
    <h1>{{ double }}</h1>
    <h1>{{ greetings }}</h1>
    <p>{{error}}</p>
    <Suspense>
      <template #default>
        <async-show />
        <dog-show />
      </template>
      <template #fallback>
        <h1>Loading !...</h1>
      </template>
    </Suspense>
    <h1 v-if="loading">Loading...</h1>
    <img v-if="loaded" :src="result[0].url" alt="">
    <h1>X: {{x}}, Y: {{y}}</h1>
    <button @click="openModal">open Modal</button>
    <br>
    <modal :isOpen="modalIsOpen" @close-modal="onModalClose">My modal!!!</modal>
    <button @click="increase">❤+1</button>
    <br>
    <hr>
    <button @click="updateGreeting">update Title</button>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  ref,
  computed,
  reactive,
  toRefs,
  onMounted,
  onUnmounted,
  watch,
  onErrorCaptured
} from "vue";
import useMousePosition from './Hooks/useMousePosition'
import useURLLoader from './Hooks/useURLLoader'
import modal from './components/Modal.vue'
import AsyncShow from './components/AsyncShow.vue'
import DogShow from './components/DogShow.vue'
interface DataProps {
  count: number;
  double: number;
  increase: () => void;
  numbers: number[];
  person: { name?: string };
}
interface DogResult {
  message: string;
  status: string;
}
interface CatResult {
  id: string;
  url: string;
  width: number;
  height: number;
}
export default defineComponent({
  name: "App",
  setup() {
    const error = ref(null)
    onErrorCaptured((e: any) => {
      error.value = e
      return true
    })
    const data: DataProps = reactive({
      count: 0,
      increase: () => {
        data.count++;
      },
      double: computed(() => data.count * 2),
      numbers: [0, 1, 2],
      person: {},
    });
    const {x, y} = useMousePosition()
    const {result, loading, loaded} = useURLLoader<CatResult[]>('https://api.thecatapi.com/v1/images/search?limit=1')
    watch(result, () => {
      if(result.value) {
        console.log('value', result.value[0])
      }
    })
    const greetings = ref("");
    const updateGreeting = () => {
      greetings.value += "Hello!";
    };
    
    watch([greetings, () => data.count], (newValue, oldValue) => {
      console.log('old', oldValue)
      console.log('new', newValue)
      document.title = "updated" + greetings.value + data.count;
    }) 
    
    data.numbers[0] = 5;
    data.person.name = "viking";
    const refData = toRefs(data);
    const modalIsOpen = ref(false)
    const openModal = () => {
      modalIsOpen.value = true
    }
    const onModalClose = () => {
      modalIsOpen.value = false
    }
    return {
      ...refData,
      greetings,
      updateGreeting,
      x,
      y,
      result,
      loading,
      loaded,
      modalIsOpen,
      openModal,
      onModalClose,
      error
    };
  },
  components: {
    modal,
    AsyncShow,
    DogShow
  }
});
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
